package com.example.skillcinema.ui.screens.main.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.skillcinema.db.displaymodels.FilmDisplayModel
import com.example.skillcinema.db.repositories.BaseRepository
import com.example.skillcinema.db.repositories.FilmRepository
import com.example.skillcinema.db.responses.PersonResponse
import dagger.hilt.android.lifecycle.HiltViewModel
import jakarta.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

@HiltViewModel
class StaffDetailViewModel @Inject constructor(val repository: FilmRepository) : ViewModel() {
    private var _staffDetailStateFlow: MutableStateFlow<PersonResponse?> =
        MutableStateFlow<PersonResponse?>(null)
    val staffDetailFlow = _staffDetailStateFlow.asStateFlow()
    private var _staffBestFilmStateFlow: MutableStateFlow<List<FilmDisplayModel>> =
        MutableStateFlow<List<FilmDisplayModel>>(listOf())
    val staffBestFilmFlow = _staffBestFilmStateFlow.asStateFlow()
    private var _staffFilmographyStateFlow: MutableStateFlow<Int> =
        MutableStateFlow<Int>(0)
    val staffFilmographyFlow = _staffFilmographyStateFlow.asStateFlow()

    fun loadData(id: Int) {
        viewModelScope.run {
            launch {
                _staffDetailStateFlow.value = repository.getStaffDetail(id)
            }
            launch {
                _staffBestFilmStateFlow.value =
                    repository.getStaffBestFilms(id, BaseRepository.FIRST_PAGE)
            }
            launch {
                _staffFilmographyStateFlow.value =
                    repository.getStaffFilmographyCount(id)
            }
        }
    }
}